package com.example.administrator.yuejian35.Home.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Config;
import com.example.administrator.yuejian35.Home.HomeModel;
import com.example.administrator.yuejian35.Home.bean.Activity;
import com.example.administrator.yuejian35.Home.bean.Result;
import com.example.administrator.yuejian35.MainActivity;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.base.BaseActivity;
import com.example.administrator.yuejian35.button.ShineButton;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ActivitysActivity extends BaseActivity {
    private TextView aname,intro,adress,time,phone,smoney,applypeaple,amoney;
    private ImageView pic;
    private Context context;
    private Activity activitys;
    private Button apply;
    private ShineButton shoucang;

    private String type,Uid,id,aid,name,notes;
    private int am,sm;

    private SharedPreferences settings,sp;
    private SharedPreferences.Editor editor,editor1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activitys);

        aname = (TextView) findViewById(R.id.activity_aname);
        intro = (TextView) findViewById(R.id.activity_intro);
        adress = (TextView) findViewById(R.id.activity_aadress);
        time = (TextView) findViewById(R.id.activity_atime);
        phone = (TextView) findViewById(R.id.activity_phone);
        smoney = (TextView) findViewById(R.id.activity_smoney);
        applypeaple = (TextView) findViewById(R.id.activity_applyp);
        amoney = (TextView) findViewById(R.id.activity_amoney);
        pic = (ImageView) findViewById(R.id.activity_pic);
        apply = (Button) findViewById(R.id.button_apply);
        shoucang = (ShineButton) findViewById(R.id.activity_heartview);

        MainActivity mainActivity = new MainActivity();
        Uid = mainActivity.getUserId();

        settings = getSharedPreferences(Uid, 0);
        editor = settings.edit();

        sp = getSharedPreferences(Uid, Context.MODE_PRIVATE);
        editor1 = sp.edit();
        editor1.putString("aaa","111");
        editor1.commit();

        String aaa = sp.getString("aaa","");

        Log.v("aaa",aaa);

        if (shoucang != null)
            shoucang.init(this);

        activitys = getIntent().getParcelableExtra("activitys");

        if (activitys!=null){
            aid = activitys.getAid();
            name = activitys.getAname();
            am = Integer.parseInt(activitys.getAmoney());
            sm = Integer.parseInt(activitys.getSmoney());
            notes = activitys.getNotes();

            aname.setText(activitys.getAname());
            intro.setText(activitys.getContent());
            adress.setText(activitys.getAddress());
            time.setText(activitys.getDate());
            phone.setText(activitys.getTel());
            smoney.setText(activitys.getSmoney());
            amoney.setText(activitys.getAmoney());
            applypeaple.setText(String.valueOf(activitys.getPsum()));

            if(!activitys.getPic().equals("")){
                Picasso.with(context)
                        .load(Config.ActivityImages_URL+activitys.getPic())
                        .into(pic);
            }

        }



        apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(ActivitysActivity.this,ApplyActivity.class);
                intent.putExtra("aid",aid);
                intent.putExtra("aname",name);
                intent.putExtra("sm",sm);
                intent.putExtra("am",am);
                intent.putExtra("notes",notes);
                ActivitysActivity.this.startActivity(intent);

            }
        });

        shoucang.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(View view, final boolean checked) {
                //Log.e(TAG, "click " + checked);
                if(checked==true){
                    //final String aid =activitys.getAid();
                    HomeModel homeModel = new HomeModel();
                    Call<Result> call = homeModel.collect(aid,Uid,"true","");
                    call.enqueue(new Callback<Result>() {
                        @Override
                        public void onResponse(Call<Result> call, Response<Result> response) {
                            Toast.makeText(ActivitysActivity.this,"收藏成功！！！",Toast.LENGTH_SHORT).show();
                            id = response.body().getResult();
                            editor1.putString("activity_collect","true");
                            editor1.putString(aid,aid);
                            editor1.commit();

                        }

                        @Override
                        public void onFailure(Call<Result> call, Throwable t) {
                            Log.v("联网","失败！！！");
                        }
                    });
                }

                else if(checked==false){
                    //String aid =activitys.getAid();
                    HomeModel homeModel = new HomeModel();
                    Call<Result> call = homeModel.collect(aid,Uid,"false",id);
                    call.enqueue(new Callback<Result>() {
                        @Override
                        public void onResponse(Call<Result> call, Response<Result> response) {
                            Toast.makeText(ActivitysActivity.this,"取消收藏成功！！！",Toast.LENGTH_SHORT).show();
                            //type = String.valueOf(checked);
                            editor1.putString("activity_collect","false");
                            editor1.putString(aid,aid);
                            editor1.commit();
                        }

                        @Override
                        public void onFailure(Call<Result> call, Throwable t) {
                            Log.v("联网","失败！！！");
                        }
                    });

                }
            }

        });

        String bbb = sp.getString("activity_collect","-1");
        String acid = settings.getString(aid,"-1");

        if(bbb.equals("true") && acid.equals(aid)){
            shoucang.setChecked(true);
        }

    }
}
